package com.ruoyi.receipt.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.receipt.utils.UserInitUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.receipt.mapper.TbKnowledgeListMapper;
import com.ruoyi.receipt.domain.TbKnowledgeList;
import com.ruoyi.receipt.service.ITbKnowledgeListService;

import javax.swing.tree.TreePath;

/**
 * 知识点目录管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-03-27
 */
@Service
public class TbKnowledgeListServiceImpl implements ITbKnowledgeListService 
{
    @Autowired
    private TbKnowledgeListMapper tbKnowledgeListMapper;

    /**
     * 查询知识点目录管理
     * 
     * @param id 知识点目录管理ID
     * @return 知识点目录管理
     */
    @Override
    public TbKnowledgeList selectTbKnowledgeListById(Long id)
    {
        return tbKnowledgeListMapper.selectTbKnowledgeListById(id);
    }

    @Override
    public List<TbKnowledgeList> getTbKnowledgeTreeList() {
        //获取所有的一级分类
        List<TbKnowledgeList> tbKnowledgeLists = listWithTree();
        return tbKnowledgeLists;
    }
    public List<TbKnowledgeList> listWithTree() {
        //1、查出所有知识点目录(这里查询的是已经审核的目录 只有已经审核的目录才能添加知识点)
        TbKnowledgeList tbKnowledgeList = new TbKnowledgeList();
        tbKnowledgeList.setStatus(2);
        List<TbKnowledgeList> entities = tbKnowledgeListMapper.selectTbKnowledgeListList(tbKnowledgeList);

        //2、组装成父子的树形结构

        //2.1）、找到所有的一级目录（一级目录的parentId都是0）

        List<TbKnowledgeList> level1Menus = entities.stream().filter(knowledge ->
                knowledge.getParentId() == 0
        ).map((menu)->{
            menu.setChildren(getChildrens(menu,entities));
            return menu;
        })/*.sorted((menu1,menu2)->{
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        })*/.collect(Collectors.toList());

        return level1Menus;
    }

    //递归查找所有目录的子目录
    private List<TbKnowledgeList> getChildrens(TbKnowledgeList root,List<TbKnowledgeList> all){
        List<TbKnowledgeList> children = all.stream().filter(knowledge -> {
            return knowledge.getParentId() == root.getId();
        }).map(knowledge -> {
            //1、找到子目录
            knowledge.setChildren(getChildrens(knowledge,all));
            return knowledge;
        })/*.sorted((menu1,menu2)->{
            //2、菜单的排序
            //return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        })*/.collect(Collectors.toList());
        return children;
    }

    /**
     * 查询知识点目录管理列表
     * 
     * @param tbKnowledgeList 知识点目录管理
     * @return 知识点目录管理
     */
    @Override
    public List<TbKnowledgeList> selectTbKnowledgeListList(TbKnowledgeList tbKnowledgeList)
    {
        return tbKnowledgeListMapper.selectTbKnowledgeListList(tbKnowledgeList);
    }

    @Override
    public List<TbKnowledgeList> duplicateValidation(TbKnowledgeList tbKnowledgeList) {
        return tbKnowledgeListMapper.duplicateValidation(tbKnowledgeList);
    }

    @Override
    public List<TreeSelect> buildKnowledgeTreeSelect(List<TbKnowledgeList> tbKnowledgeList) {
        List<TreeSelect> lists = new ArrayList<>();
        List<TbKnowledgeList> konwledgeTrees = buildKnowledgeTree(tbKnowledgeList);
        //deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
        //List<TreeSelect> collect = konwledgeTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
        //return collect;
        return lists;
    }

    @Override
    public List<TbKnowledgeList> buildKnowledgeTree(List<TbKnowledgeList> tbKnowledgeList) {
        List<TbKnowledgeList> returnList = new ArrayList<TbKnowledgeList>();
        List<Long> tempList = new ArrayList<Long>();
        for (TbKnowledgeList knowledge : tbKnowledgeList)
        {
            tempList.add(knowledge.getId());
        }
        for (Iterator<TbKnowledgeList> iterator = tbKnowledgeList.iterator(); iterator.hasNext();)
        {
            TbKnowledgeList konwledge = (TbKnowledgeList) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(konwledge.getParentId()))
            {
                recursionFn(tbKnowledgeList, konwledge);
                returnList.add(konwledge);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = tbKnowledgeList;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<TbKnowledgeList> list, TbKnowledgeList t)
    {
        // 得到子节点列表
        List<TbKnowledgeList> childList = getChildList(list, t);
        t.setChildren(childList);
        for (TbKnowledgeList tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<TbKnowledgeList> list, TbKnowledgeList t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

    /**
     * 得到子节点列表
     */
    private List<TbKnowledgeList> getChildList(List<TbKnowledgeList> list, TbKnowledgeList t)
    {
        List<TbKnowledgeList> tlist = new ArrayList<TbKnowledgeList>();
        Iterator<TbKnowledgeList> it = list.iterator();
        while (it.hasNext())
        {
            TbKnowledgeList n = (TbKnowledgeList) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 新增知识点目录管理
     * 
     * @param tbKnowledgeList 知识点目录管理
     * @return 结果
     */
    @Override
    public int insertTbKnowledgeList(TbKnowledgeList tbKnowledgeList)
    {
        tbKnowledgeList.setCreateTime(DateUtils.getNowDate());
        UserInitUtils.initUserInfo(tbKnowledgeList);
        return tbKnowledgeListMapper.insertTbKnowledgeList(tbKnowledgeList);
    }

    /**
     * 修改知识点目录管理
     * 
     * @param tbKnowledgeList 知识点目录管理
     * @return 结果
     */
    @Override
    public int updateTbKnowledgeList(TbKnowledgeList tbKnowledgeList)
    {
        tbKnowledgeList.setUpdateTime(DateUtils.getNowDate());
        //修改后将审核状态改成未审核
        tbKnowledgeList.setStatus(1);
        UserInitUtils.initUserInfoForUpdate(tbKnowledgeList);
        return tbKnowledgeListMapper.updateTbKnowledgeList(tbKnowledgeList);
    }

    /**
     * 知识点目录审核
     *
     * @param tbKnowledgeList 知识点目录审核
     * @return 结果
     */
    @Override
    public int updateKnowledgeAudit(TbKnowledgeList tbKnowledgeList)
    {
        tbKnowledgeList.setUpdateTime(DateUtils.getNowDate());
        //修改后将审核状态改成未审核
        return tbKnowledgeListMapper.updateTbKnowledgeList(tbKnowledgeList);
    }

    /**
     * 批量删除知识点目录管理
     * 
     * @param ids 需要删除的知识点目录管理ID
     * @return 结果
     */
    @Override
    public int deleteTbKnowledgeListByIds(Long[] ids)
    {
        return tbKnowledgeListMapper.deleteTbKnowledgeListByIds(ids);
    }



    @Override
    public List<Map<String,Object>> selectKnowledgeByKnowledgeIds(Long[] ids)
    {
        return tbKnowledgeListMapper.selectKnowledgeByKnowledgeIds(ids);
    }

    /**
     * 删除知识点目录管理信息
     * 
     * @param id 知识点目录管理ID
     * @return 结果
     */
    @Override
    public int deleteTbKnowledgeListById(Long id)
    {
        return tbKnowledgeListMapper.deleteTbKnowledgeListById(id);
    }

    @Override
    public int delListLogic(Long[] id) {
        return tbKnowledgeListMapper.delListLogic(id);
    }
}
